<?php

namespace App\Admin\Controllers;


use App\Admin\Renderable\BomListTable;
use App\Models\Product;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Log;

class ProductController extends AdminController
{

//    public function show($id, Content $content)
//    {
//        return $content->title($this->title);
//    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Product(), function (Grid $grid) {

            $grid->column('product_number');
            $grid->column('product_name');
            $grid->column('master_img')->image(env('APP_URL').'/storage','70','100');


            $grid->column('customer_number');
            $grid->column('product_type');
            $grid->column('type');
            $grid->column('product_line');
            $grid->column('price');
            $grid->column('pattern');
            $grid->column('research_dept');
            $grid->column('research_person');
            $grid->column('bom')->modal(function ($modal){
                $modal->title('BOM结构列表');

                return BomListTable::make()->payload(['id'=>$this->id]);
            });;
            $grid->column('raw_material');
            $grid->column('raw_material_rate');
            $grid->column('color');
            $grid->column('matching');
            $grid->column('wool_length');
            $grid->column('length');
            $grid->column('weight');

//            $grid->column('master_img')->gallery();
            $grid->column('imgs');
            $grid->column('videos');
            $grid->column('job_instruction');
            $grid->column('trademark');
            $grid->column('labels');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            //启动边框模式
            $grid->fixColumns(4);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('product_number')->select(Product::query()->pluck('product_number','product_number'))->width(3);
                $filter->equal('product_name')->select(Product::query()->pluck('product_name','product_name'))->width(3);
                $filter->equal('customer_number')->select(Product::query()->pluck('customer_number','customer_number'))->width(3);
                $filter->equal('product_type')->select(Product::query()->pluck('product_type','product_type'))->width(3);
                $filter->equal('type')->select(Product::query()->pluck('type','type'))->width(3);
                $filter->equal('product_line')->select(Product::query()->pluck('product_line','product_line'))->width(3);
                $filter->between('price')->width(3);
                $filter->equal('pattern')->select(Product::query()->pluck('pattern','pattern'))->width(3);
                $filter->equal('research_dept')->select(Product::query()->pluck('research_dept','research_dept'))->width(3);
                $filter->between('created_at')->datetime()->width(3);

            });

            //按钮控制
//            $grid->enableDialogCreate();//快捷编辑
            /**
             * 字段过滤
             */
            $grid->showColumnSelector();
            $grid->hideColumns([
                'bom',
                'research_dept'
            ]);

            /**
             * 快速搜索.
             */
            $grid->quickSearch('product_number','product_name')
                ->placeholder('快速搜索一下')
                ->auto(false);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Product(), function (Show $show) {

            $show->field('product_number');
            $show->field('product_name');
            $show->field('customer_number');
            $show->field('product_type');
            $show->field('type');
            $show->field('product_line');
            $show->field('price');
            $show->field('pattern');
            $show->field('research_dept');
            $show->field('research_person');
            $show->field('bom');
            $show->field('raw_material');
            $show->field('raw_material_rate');
            $show->field('color');
            $show->field('matching');
            $show->field('wool_length');
            $show->field('length');
            $show->field('weight');
            $show->field('master_img')->image();
            $show->field('imgs')->image();
            $show->field('videos')->explode(',')->view('admin.show_video');
            $show->field('job_instruction')->image();
            $show->field('trademark')->image();
            $show->field('labels');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     * @return Form
     *
     */
    protected function form()
    {

        return Form::make(new Product(), function (Form $form) {
            $labels = [
                '发条',
                '发块',
                '男士',
                '女士',
            ];
            $form->row(function (Form\Row $row) use ($form,$labels){
                $product_type = [

                ];


                $row->width(6)->text('product_number');
                $row->width(6)->text('product_name');
                $row->width(6)->text('customer_number');
                $row->width(6)->text('product_type');
                $row->width(6)->text('type');
                $row->width(6)->text('product_line');
                $row->width(6)->text('price');
                $row->width(6)->text('pattern');
                $row->width(6)->text('research_dept');
                $row->width(6)->text('research_person');
                $row->width(12)->table('bom',function (Form\NestedForm $table){
                    $table -> text('sap_number')->label('物料号');
                    $table -> text('sap_desc')->label('物料描述');
                    $table -> text('qty')->label('数量');
                });
                $row->width(6)->text('raw_material');
                $row->width(6)->text('raw_material_rate');
                $row->width(6)->text('color');
                $row->width(6)->text('matching');
                $row->width(6)->text('wool_length');
                $row->width(6)->text('length');
                $row->width(6)->text('weight');
                $row->width(12)
                    ->image('master_img')
                    ->autoUpload()
                    ->uniqueName()
                    ->help('首图上传只能上传一张');
//                if ($form->isEditing()) {}
                $row->width(12)
                    ->multipleImage('imgs')
                    ->autoUpload()
                    ->uniqueName();
//                $row->width(6)
//                    ->multipleFile('videos')
//                    ->maxSize(102400)
//                    ->autoUpload()
//                    ->uniqueName();
                $row->width(12)->mediaSelector('videos')->limit(10);
                $row->width(6)
                    ->multipleImage('job_instruction')
                    ->autoUpload()
                    ->uniqueName();
                $row->width(6)
                    ->multipleImage('trademark')
                    ->autoUpload()
                    ->uniqueName();
                $row->width(6)->tags('labels')->options($labels);
            });
            $form->submitted(function (Form $form){
//                Log::info($form->input());
            });

        });
    }
}
